package com.quanxiaoha.weblog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quanxiaoha.weblog.admin.model.vo.announcement.*;
import com.quanxiaoha.weblog.admin.service.AdminAnnouncementService;
import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.domain.dos.AnnouncementDO;
import com.quanxiaoha.weblog.common.domain.dos.AnnouncementVO;
import com.quanxiaoha.weblog.common.domain.mapper.AnnouncementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminAnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, AnnouncementDO> implements AdminAnnouncementService {

    @Resource
    private AnnouncementMapper announcementMapper;

    /**
     * 保存公告
     *
     * @param saveAnnouncementReqVO
     * @return
     */
    @Override
    public Response saveAnnouncement(SaveAnnouncementReqVO saveAnnouncementReqVO) {
        AnnouncementDO announcementDO = AnnouncementDO.builder()
                .id(saveAnnouncementReqVO.getId())
                .isEnabled(saveAnnouncementReqVO.getIsEnabled())
                .updateTime(LocalDateTime.now())
                .build();

        // 只有在content不为空时才设置content字段
        if (StringUtils.hasText(saveAnnouncementReqVO.getContent())) {
            announcementDO.setContent(saveAnnouncementReqVO.getContent());
        }

        // 如果是更新操作
        if (Objects.nonNull(saveAnnouncementReqVO.getId())) {
            announcementDO.setCreateTime(null);
        } else {
            // 新增时设置创建时间
            announcementDO.setCreateTime(LocalDateTime.now());
        }

        // 保存或更新公告
        saveOrUpdate(announcementDO);
        return Response.success();
    }

    /**
     * 删除公告
     *
     * @param deleteAnnouncementReqVO
     * @return
     */
    @Override
    public Response deleteAnnouncement(DeleteAnnouncementReqVO deleteAnnouncementReqVO) {
        // 物理删除公告
        removeById(deleteAnnouncementReqVO.getId());
        return Response.success();
    }

    /**
     * 获取公告分页列表
     *
     * @param findAnnouncementPageListReqVO
     * @return
     */
    @Override
    public PageResponse<AnnouncementVO> findAnnouncementPageList(FindAnnouncementPageListReqVO findAnnouncementPageListReqVO) {
        // 设置分页参数
        Page<AnnouncementDO> page = new Page<>(findAnnouncementPageListReqVO.getCurrent(), findAnnouncementPageListReqVO.getSize());

        // 构建查询条件
        LambdaQueryWrapper<AnnouncementDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(findAnnouncementPageListReqVO.getContent()),
                        AnnouncementDO::getContent, findAnnouncementPageListReqVO.getContent())
                .eq(StringUtils.hasText(findAnnouncementPageListReqVO.getIsEnabled()),
                        AnnouncementDO::getIsEnabled, findAnnouncementPageListReqVO.getIsEnabled())
                .eq(AnnouncementDO::getIsDeleted, 0) // 只查询未删除的
                .orderByDesc(AnnouncementDO::getCreateTime); // 按创建时间倒序

        // 执行分页查询
        IPage<AnnouncementDO> pageResult = announcementMapper.selectPage(page, wrapper);

        // 转换DO为VO，使用AnnouncementVO
        List<AnnouncementVO> announcementVOList = pageResult.getRecords().stream()
                .map(announcementDO -> {
                    AnnouncementVO announcementVO = new AnnouncementVO();
                    BeanUtils.copyProperties(announcementDO, announcementVO); // 修复映射方向
                    return announcementVO;
                })
                .collect(Collectors.toList());

        return PageResponse.success(pageResult, announcementVOList);
    }

    /**
     * 获取公告详情
     *
     * @param findAnnouncementDetailReqVO
     * @return
     */
    @Override
    public Response<AnnouncementVO> findAnnouncementDetail(FindAnnouncementDetailReqVO findAnnouncementDetailReqVO) {
        // 查询公告详情
        AnnouncementDO announcementDO = announcementMapper.selectById(findAnnouncementDetailReqVO.getId());

        // 检查公告是否存在且未删除
        if (Objects.isNull(announcementDO) || Objects.equals(announcementDO.getIsDeleted(), 1)) {
            return Response.fail("公告不存在");
        }

        // 转换为VO对象
        AnnouncementVO announcementVO = new AnnouncementVO();
        BeanUtils.copyProperties(announcementDO, announcementVO);

        return Response.success(announcementVO);
    }
}